package com.clz.hyperit.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.clz.hyperit.dao.CollectionDao;
import com.clz.hyperit.entity.Collection;
import com.clz.hyperit.vo.PageInfo;

@Repository("collectionDao")
public class CollectionDaoImpl extends BaseDaoImpl<Collection> implements
		CollectionDao {

	public void deleteByArticleId(Integer article_id) {
		String hql = "delete from Collection where goal_id = :goal_id and type = 0";
		getCurrentSession().createQuery(hql)
				.setInteger("goal_id", article_id).executeUpdate();
	}

	public void delete(Integer userdetail_id, Integer id, Integer type) {
		String hql = "delete from Collection where goal_id = :goal_id and userdetail_id = :userdetail_id and type = :type";
		getCurrentSession().createQuery(hql)
		.setInteger("goal_id", id).setInteger("userdetail_id", userdetail_id).setInteger("type", type).executeUpdate();
	}

	public Collection getBy_articleId_userdetail_id(Integer article_id,
			Integer userdetail_id) {
		String hql = " from Collection where goal_id = :goal_id and userdetail_id = :userdetail_id and type = 0";
		return (Collection) getCurrentSession().createQuery(hql)
		.setInteger("goal_id", article_id).setInteger("userdetail_id", userdetail_id).uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Collection> findByUserId(Integer userdetail_id,PageInfo pageInfo,Integer type) {
		String hql = "from Collection where userdetail_id = :userdetail_id and type = :type order by createtime desc";
		List<Collection> list =  (List<Collection>) getCurrentSession().createQuery(hql)
				.setInteger("userdetail_id", userdetail_id)
				.setInteger("type", type)
				.setFirstResult((pageInfo.getIndexPageNum() - 1) * pageInfo.getSize())	
				.setMaxResults(pageInfo.getSize())
				.list();
		// 获得表中总的条目数目
				String hql2 = "select count(*) from Collection where userdetail_id = :userdetail_id and type = :type";
				Object uniqueResult = getCurrentSession().createQuery(hql2)
						.setInteger("userdetail_id", userdetail_id)
						.setInteger("type", type)
						.uniqueResult();
				Long intValue = (new Integer(uniqueResult.toString())).longValue();
				pageInfo.setTotalCount(intValue);
				return list;
	}

	public void deleteByNoteId(Integer note_id) {
		String hql = "delete from Collection where goal_id = :goal_id and type = 1";
		getCurrentSession().createQuery(hql)
				.setInteger("goal_id", note_id).executeUpdate();
	}

	public Collection getbyNoteAndUd(Integer note_id, Integer userdetail_id) {
		String hql = " from Collection where goal_id = :goal_id and userdetail_id = :userdetail_id and type = 1";
		return (Collection) getCurrentSession().createQuery(hql)
		.setInteger("goal_id", note_id).setInteger("userdetail_id", userdetail_id).uniqueResult();
	}
}
